﻿using System.IO;
using System.Net;
using System.Text;

namespace Google.Translates.Core
{
    public class HttpModelHelper
    {
        internal string GetResultStringByRequestPostForGoogle(string strUrl)
        {
            var innerRequest = (HttpWebRequest) WebRequest.Create(strUrl);
            innerRequest.Headers.Add("X-HTTP-Method-Override", "POST");
            var innerResponse = (HttpWebResponse) innerRequest.GetResponse();
            Stream rStream = innerResponse.GetResponseStream();
            if (rStream == null) return null;
            var respStream = new StreamReader(rStream, Encoding.UTF8);
            string resultString;
            try
            {
                resultString = respStream.ReadToEnd();
            }
            finally
            {
                respStream.Dispose();
                respStream.Close();
                innerResponse.Close();
            }
            return resultString;
        }

        internal string GetResultStringByRequestPostForBing(string strUrl)
        {
            
            var innerRequest = (HttpWebRequest)WebRequest.Create(strUrl);
            var innerResponse = (HttpWebResponse)innerRequest.GetResponse();
            innerRequest.ContentType = "text/xml";
            innerRequest.Method = "POST";
            Stream rStream = innerResponse.GetResponseStream();
            
            if (rStream == null) return null;
            var respStream = new StreamReader(rStream, Encoding.UTF8);
            string resultString;
            try
            {
                resultString = respStream.ReadToEnd();
            }
            finally
            {
                respStream.Dispose();
                respStream.Close();
                innerResponse.Close();
            }
            return resultString;
        }
    }
}